/**
 * @Description: 通用组件
 * @Copyright: 2017 www.fallsea.com Inc. All rights reserved.
 * @author: fallsea
 * @version 1.8.2
 * @License：MIT
 */

//(function () {
	//layui.define(['layer'], function (exports) {
		var common = {
			version: "1.0.1",
			/** 刷新token时间  */
			refreshMinutes: 1000 * 60 * 10,
			/**错误msg提示 */
			errorMsg: function (text) {
				layer.msg(text, { icon: 2, time: 1000 });
			},
			/**成功 msg提示 */
			successMsg: function (text) {
				layer.msg(text, { icon: 1, time: 1000 });
			},
			/**警告弹出提示*/
			warnMsg: function (text) {
				layer.msg(text, { icon: 0, time: 1000 });
			},
			reloadMsg: function (text) {
				var promptLayer = layer.msg(text, { icon: 16, time: false, shade: 0.8 });
				return promptLayer;
			},
			closeMsg: function (promptLayer) {
				layer.close(promptLayer);
			},
			reload: function () {
				var loadLayui = layer.load(0, {
					time: false,
					shade: 0.8
				});
				return loadLayui;
			},
			closeReload: function (loadLayui) {
				layer.close(loadLayui);
			},
			confirm: function (title, text, callBackFunc, icon, cancleFunc) {
				if (!icon) {
					icon = 3
				}
				var confirmDialog = layer.confirm(text, {
					title: title,
					resize: false,
					btn: ['确定', '取消'],
					btnAlign: 'c',
					anim: 0,
					icon: icon,
					cancel: function (index, layero) {
						if (cancleFunc) {
							cancleFunc();
						}
					}
				}, function () {
					layer.close(confirmDialog);
					callBackFunc();
				}, function () {
					if (cancleFunc) {
						cancleFunc();
					}
				})

			},
			//弹出窗口
			open: function (_title, _width, _height, content, isMaximize, _end) {
				if (!_width) {
					_width = "700px";
				}
				if (!_height) {
					_height = "400px";
				}
				var index = layer.open({
					type: 2,
					title: _title,
					area: [_width, _height],
					fixed: true, //不固定
					scrollbar: true,
					maxmin: true,
					content: content,
					end: _end
				});
				if (isMaximize == "1") {
					$(window).resize(function () {
						layer.full(index);
					})
					layer.full(index);
				}
			},
			closeOpen: function () {
				var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
				parent.layer.close(index);
			},
			getUrlParam: function (name) {
				var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
				var r = window.location.search.substr(1).match(reg);
				if (r != null) {
					return unescape(r[2]);
				}
				return null;
			},
			versionUrl: function (url) {
				if (url.indexOf("?") > -1) {
					url = url + "&version=" + this.version
				} else {
					url = url + "?version=" + this.version
				}
				return url;
			},
			dateToString: function (date) {
				var month = date.getMonth() + 1;
				var strDate = date.getDate();
				if (month >= 1 && month <= 9) {
					month = "0" + month;
				}
				if (strDate >= 0 && strDate <= 9) {
					strDate = "0" + strDate;
				}
				var currentdate = date.getFullYear() + "-" + month + "-" + strDate;
				return currentdate;
			},
			dateFromString: function (dateString) {
				if (dateString) {
					var arr1 = dateString.split(" ");
					var sdate = arr1[0].split('-');
					var date = new Date(sdate[0], sdate[1] - 1, sdate[2]);
					return date;
				}
				else {
					return null;
                }
            },
			addDays: function (dateTime, days) {
				dateTime = dateTime.setDate(dateTime.getDate() + days);
				dateTime = new Date(dateTime);
				return dateTime;

            },
			//转换获取树形所有选中节点
			getTreeCheckedNode: function (checkedData) {
				var nodes = new Array();
				checkedData.forEach(node => {
					nodes.push(node);
					if (node.children && node.children.length > 0) {
						var childrenNodes = this.getTreeCheckedNode(node.children);
						nodes = nodes.concat(childrenNodes);
					}
				});
				return nodes;
			}
		};
		window.common = common;
	//})

//})();